package com.zy.qudadid.ui.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import com.zy.lcpassenger.R;
import com.zy.qudadid.model.Stroke;
import com.zy.qudadid.model.StrokeInfor;
import com.zy.qudadid.network.Const;
import com.zy.qudadid.presenter.StrokeAllPresenter;
import com.zy.qudadid.ui.adapter.base.BaseAdapter;
import com.zy.qudadid.utils.AESCrypt;
import com.zy.qudadid.utils.CarUtil;
import com.zy.qudadid.utils.StringUtil;
import com.zy.qudadid.utils.UserUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * Created by ASUS on 2017/2/17.
 */

public class StrokeAllAdapter extends BaseAdapter<StrokeAllAdapter.VHolder,StrokeInfor,StrokeAllPresenter>{

    int curPosition=-1;

    public StrokeAllAdapter(Context context) {
        super(context);
    }

    public StrokeAllAdapter(Context context, StrokeAllPresenter strokeAllPresenter) {
        super(context, strokeAllPresenter);
        isAddFooter = false;
    }

    @Override
    public int provideItemLayoutId() {
        return R.layout.item_stroke;
    }

    @Override
    public VHolder createVH(ViewGroup parent, int viewType, View view) {
        return new VHolder(view);
    }

    @Override
    public void bindData(VHolder holder, final int position) {
        StrokeInfor stroke = data.get(position);
        holder.tv_start.setText(stroke.departure);
        holder.tv_end.setText(stroke.destination);
        holder.tv_process.setText(CarUtil.getStrokeState(stroke.state));
        holder.tv_date.setText(stroke.addtime);
//        holder.oxiEnd.setText(stroke.destination);
//        if (CarUtil.getStrokeState(stroke.state).equals("已完成") || CarUtil.getStrokeState(stroke.state).equals("已取消")) {
//            holder.oxiState.setTextColor(context.getResources().getColor(R.color.gray_font_3));
//        }else  if (CarUtil.getStrokeState(stroke.state).equals("行驶中") ){
//            holder.oxiState.setTextColor(context.getResources().getColor(R.color.yellow_2));
//        }else{
//            holder.oxiState.setTextColor(context.getResources().getColor(R.color.red_btn));
//        }

        switch (stroke.state){
            case "0"://无人接单
                holder.tv_cancel.setVisibility(View.VISIBLE);
                break;
            case "1"://已接单
                holder.tv_cancel.setVisibility(View.VISIBLE);
                break;
            case "2"://乘客上车
                holder.tv_cancel.setVisibility(View.GONE);
                break;
            case "3"://已完成
                holder.tv_cancel.setVisibility(View.GONE);
                break;
            case "4"://已取消
                holder.tv_cancel.setVisibility(View.GONE);
                break;
            case "5"://待支付
                holder.tv_cancel.setVisibility(View.GONE);
                break;
            case "6"://待评价
                holder.tv_cancel.setVisibility(View.GONE);
                break;
            case "7"://司机确认支付
                holder.tv_cancel.setVisibility(View.GONE);
                break;
        }

        holder.tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                curPosition = position;
                    Map map = new HashMap();
                    map.put("order_id", data.get(curPosition).id);
                    map.put("user_id", new UserUtil(context).getUserId());
                    String json = StringUtil.toJson(map);
                    try {
                        String data1 = AESCrypt.getInstance().encrypt(json);
                        String d = StringUtil.replaceBlank(data1);
                        OkHttpUtils.post()
                                .url(Const.BASE_URL+"api.php/Order/passengerCancel_order")
                                .addParams("args",d)
                                .build()
                                .execute(new StringCallback() {
                                    @Override
                                    public void onError(Call call, Exception e, int i) {

                                    }

                                    @Override
                                    public void onResponse(String s, int i) {
                                        JSONObject json;
                                        try {
                                            json =  new JSONObject(s);
                                            int code = json.getInt("code");
                                            if(code == 200){
                                                Toast.makeText(context,json.getString("message"),Toast.LENGTH_SHORT).show();
                                                data.remove(curPosition);
                                                notifyDataSetChanged();
                                            }else{
                                                Toast.makeText(context,json.getString("message"),Toast.LENGTH_SHORT).show();
                                            }
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                });
                    } catch (Exception ex) {
                    }
            }
        });
    }

    class VHolder extends RecyclerView.ViewHolder {
        @Nullable
        @Bind(R.id.tv_start)
        TextView tv_start;
        @Nullable
        @Bind(R.id.tv_end)
        TextView tv_end;
        @Nullable
        @Bind(R.id.tv_date)
        TextView tv_date;
        @Nullable
        @Bind(R.id.tv_time)
        TextView tv_time;
        @Nullable
        @Bind(R.id.tv_process)
        TextView tv_process;
        @Nullable
        @Bind(R.id.tv_cancel)
        TextView tv_cancel;

        VHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
